{% extends 'layout/basic.html' %}
{% load static %}
{% block title %}短信验证码登录{% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
{% endblock %}
{% block content %}
    <form class="account" id="smsForm" method="post">
        {% csrf_token %}
        <div class="shell">
            <h2 class="title">Login</h2>
            {% for field in form %}
                <div class="form-group">
                    {% if field.label != '验证码' %}
                        {{ field }}
                        <div class="err-msg"></div>

                    {% else %}
                        <div class="col-md-6">
                            {{ field }}
                            <div class="err-msg"></div>
                        </div>
                        <div class="col-md-6">
                            <input type="button" class="btn-primary" id="code" value="点击获取验证码">
                        </div>
                    {% endif %}
                </div>
            {% endfor %}
            <div class="skip">
                <a href="{% url 'login' %}">账号密码登录</a>
            </div>
            <input id='btnSubmit' type="button" value="登录">
        </div>
    </form>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            bindClickSms();
            bindClickSubmit();
        });

        function bindClickSms() {
            $('#code').click(function () {
                let mobilePhone = $('#id_mobile_phone').val();
                $.ajax({
                    url: "{% url 'send_sms' %}",
                    type: "GET",
                    data: {mobile_phone: mobilePhone, tpl: 'login'},
                    dataType: "JSON",
                    success: function (res) {
                        $('.err-msg').empty();
                        if (!res.status) {
                            $.each(res.error, function (key, val) {
                                $('#id_' + key).next().text(val);
                            })
                        } else {
                            sendSmsRemind();
                        }
                    }
                })
            })
        }

        function sendSmsRemind() {
            let $code = $('#code');
            $code.prop('disabled', true);
            let time = 60;
            let remind = setInterval(function () {
                console.log(time);
                $code.val(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    $code.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)

        }

        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $.ajax({
                    url: "{% url 'login_sms' %}",
                    type: "POST",
                    data: $('#smsForm').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        $('.err-msg').empty();
                        if (res.status) {
                            location.href = res.data
                        } else {
                            $.each(res.error, function (key, val) {
                                $('#id_' + key).next().text(val);
                            })
                        }
                    }
                })
            })
        }
    </script>
{% endblock %}
